package com.example.myweather.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView

import com.example.myweather.R
import com.example.myweather.bean.WeatherApi
import com.example.myweather.tools.Weather_Dict
import com.socks.library.KLog

import java.util.Calendar
import java.util.TimeZone

import com.example.myweather.tools.Weather_Dict.switchweek

class Day_precipitation_adapter(private val mweatherApi: WeatherApi) : RecyclerView.Adapter<Day_precipitation_adapter.ViewHolder>() {

    private var mContext: Context? = null

    internal class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var cardView: CardView
        var time: TextView
        var max: TextView
        var min: TextView
        var skycon: ImageView

        init {
            cardView = view as CardView
            time = itemView.findViewById<View>(R.id.result_day_probalility_time) as TextView
            skycon = itemView.findViewById<View>(R.id.result_day_probalility_skycon) as ImageView
            max = itemView.findViewById<View>(R.id.result_day_probalility_max_value) as TextView
            min = itemView.findViewById<View>(R.id.result_day_probalility_min_value) as TextView
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Day_precipitation_adapter.ViewHolder {
        if (mContext == null) {
            mContext = parent.context
        }
        val view = LayoutInflater.from(parent.context).inflate(R.layout.card_view_day, parent, false)
        val holder = Day_precipitation_adapter.ViewHolder(view)
        holder.cardView.setOnClickListener {
            val position = holder.adapterPosition
            //Toast.makeText(mContext,String.valueOf(position),Toast.LENGTH_SHORT).show();
            //                    Count count=mCountList.get(position);
            //                Intent intent=new Intent(mContext,Exam2tActivity.class);
            //                intent.putExtra(Exam2tActivity.EXAMEXAM,exam.getExamId());
            //                intent.putExtra(Exam2tActivity.EXAMCOUNT,String.valueOf(i));
            //                intent.putExtra(Exam2tActivity.EXAMNAME,exam.getName());
            //                mContext.startActivity(intent);
        }
        return holder
    }


    override fun onBindViewHolder(holder: Day_precipitation_adapter.ViewHolder, position: Int) {

        val calendar = Calendar.getInstance()


        calendar.timeZone = TimeZone.getTimeZone("GMT+8:00")

        //小时
        val week = calendar.get(Calendar.DAY_OF_WEEK)
        KLog.d(week)


        if (position == 0) {
            holder.time.text = "现在"
        } else {
            holder.time.text = getWeek(week - 1, position)
        }

        //holder.time.setText(String.valueOf(position));
        holder.skycon.setImageBitmap(Weather_Dict.skycon_dictm(mweatherApi.result.daily.skycon[position].value))
        //holder.skycon.setText(Weather_Dict.skycon_dict(mweatherApi.getResult().getDaily().getSkycon().get(position).getValue()));
        holder.max.text = mweatherApi.result.daily.temperature[position].max.toString()
        holder.min.text = mweatherApi.result.daily.temperature[position].min.toString()


    }


    override fun getItemCount(): Int {
        return mweatherApi.result.daily.temperature.size
    }

    fun getWeek(w: Int, p: Int): String {
        var w = w
        KLog.d(w.toString() + "位置" + p.toString())
        if (w + p > 7) {

            w = w + p - 7


        } else {
            w = w + p

        }
        return "星期" + switchweek(w)

    }

}
